package com.luan.xiaopozhan.controller;

import com.luan.xiaopozhan.entity.RestResult;
import com.luan.xiaopozhan.entity.User;
import com.luan.xiaopozhan.mapper.UserMapper;
import com.luan.xiaopozhan.util.APIResultUtil;
import com.luan.xiaopozhan.util.MybaitisUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "用户相关")
@RestController
@RequestMapping("/api")
public class UserApiController {


    @ApiOperation("用户列表")
    @GetMapping("/getUserList")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "name",value = "姓名",required = true,paramType = "query"),
//            @ApiImplicitParam(name = "age",value = "年龄",required = true,paramType = "query",dataType = "Integer")
//    })
    @ApiResponses(value = {
            @ApiResponse(responseCode ="400",description ="请求参数没填好"),
            @ApiResponse(responseCode  = "200", description ="测试",content = {
                    @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = RestResult.class)))
            }),
            })
    @ResponseBody
    public RestResult<User> GetUserList() {
        try {
            SqlSession sqlSession = MybaitisUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            User userList = userMapper.selectUserList();
            sqlSession.close();
            if (userList != null) {
                return RestResult.success().data(userList);
            } else {
                return RestResult.success().data(null);
            }
        } catch (Exception e) {
            System.out.println("程序错误" + e);
            return null;
        }
    }
}
